<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;

class ClcwAnnounceLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $clcwAnnounceModel;

    public function _initialize()
    {
        $this->clcwAnnounceModel = D('ClcwAnnounce', 'Model');
    }

    /**
     * 获取公告列表
     *
     * @param type $page
     * @param type $size
     * @param type $params
     *
     * @return array
     */
    public function getClcwAnnounceList($page, $size, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 20 : (int)$size;
        $map = [];
        if (!empty($params['an_title'])) {
            $map['an_title'] = ['like', "%" . $params['an_title'] . "%"];
        }
        $cols = "id,emp_id,status,an_title,an_content,create_time,publish_time";
        $total = $this->clcwAnnounceModel->field($cols)
            ->where($map)
            ->count();
        $list = $this->clcwAnnounceModel->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order('id DESC')
            ->select();

        $empId = [];
        foreach ($list as $v) {
            $empId[] = $v['emp_id'];
        }

        array_map('strip_tags', $list);
        $result = [
            'list'   => $list,
            'total'  => $total,
            'emp_id' => join(',', $empId)
        ];
        return $result;
    }

    /**
     * 获取公告详情
     *
     * @param int $id
     *
     * @return array
     */
    public function getClcwAnnounce($id)
    {
        if (empty($id)) {
            return false;
        }
        return $this->clcwAnnounceModel->where(['id' => $id])->find();
    }

    /**
     * 添加公告数据
     *
     * @param array $data
     */
    public function addClcwAnnounce($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['create_time'] = NOW_TIME;
        return $this->clcwAnnounceModel->add($data);
    }

    /**
     * 编辑公告
     */
    public function saveClcwAnnounce($id, $data = [])
    {
        if (empty($id) || empty($data)) {
            return false;
        }
        $data['update_time'] = NOW_TIME;
        return $this->clcwAnnounceModel->where(['id' => $id])->save($data);
    }
}